Skip to content

Support the openapi-3.0 target in Schema.toStandardJSONSchemaV1 - #11355

Open
MichaelDeBoey wants to merge 3 commits into
Effect-TS:v4/next-minorfrom
MichaelDeBoey:feat/openapi-3-0-standard-json-schema
Open

MichaelDeBoey wants to merge 3 commits into
Effect-TS:v4/next-minorfrom
MichaelDeBoey:feat/openapi-3-0-standard-json-schema

Conversation

@MichaelDeBoey

@MichaelDeBoey MichaelDeBoey commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #10123, now with a consumer that handles referenced schemas.

Schema.toStandardJSONSchemaV1 throws Unsupported target: openapi-3.0 for the third target the Standard JSON Schema spec names. toBaseStandardJSONSchemaV1 now converts the draft 2020-12 document to an OpenAPI 3.0.3 Schema Object for this target, for both input and output:

Schema.toStandardJSONSchemaV1(Schema.NullOr(Schema.String))["~standard"].jsonSchema.input({ target: "openapi-3.0" })
// { type: "string", nullable: true }

Why it's worth having

#10123 was declined because the integration it showed only covered inline schemas, and references needed consumer changes. @nestjs/swagger already makes those changes. Its Standard Schema converter asks every vendor for target: "openapi-3.0", since it builds OpenAPI 3.0.0 documents, then moves the returned definitions into components.schemas and points the $refs there (normalizeConvertedSchema). Since @nestjs/swagger 12.1.0 it also reads function-shaped schemas (nestjs/swagger#4230), which is what Schema.toStandardJSONSchemaV1 returns.

With this branch and @nestjs/swagger@12.1.3, Effect schemas give an OpenAPI 3.0.0 document that passes oas-validator@5.0.8 without any extra code, with every $ref resolving to components.schemas, as zod@4.6.7 does there: a named property, one schema shared by two properties and across endpoints, recursive and mutually recursive named schemas, and a named root.

const Address = Schema.Struct({ city: Schema.String, zip: Schema.NullOr(Schema.String) }).annotate({ identifier: "Address" })
const Order = Schema.toStandardJSONSchemaV1(Schema.toStandardSchemaV1(Schema.Struct({ billing: Address, shipping: Address })))

@Controller("orders")
export class OrdersController {
  @Post()
  @ApiResponse({ status: 201, standardSchema: Order })
  create(@Body({ schema: Order }) order: typeof Order.Type) {
    return order
  }
}

On v4/next-minor, @nestjs/swagger logs Skipping a "effect" standard schema that could not be converted to OpenAPI 3.0 (Unsupported target: openapi-3.0) and documents the body as Object, without a response schema. AdonisJS's @foadonis/openapi asks for the same target.

On bundle size, measured with packages/tools/bundle (Rollup, minified, gzipped):

Fixture v4/next-minor This PR
Schema.toJsonSchemaDocument 26.83 KB 26.83 KB
Schema.toStandardJSONSchemaV1, draft-2020-12 target 28.96 KB 30.45 KB (+1.49 KB)

Code that doesn't call Schema.toStandardJSONSchemaV1 doesn't change. Code that does carries every target, since the target is only known when input() or output() is called, as it already carries the draft-07 branch.

Conversion

The conversion is a new internal JsonSchema.toStandaloneOpenApi3_0, an adapter on top of the existing Draft-04 one, so const becomes a single-value enum and exclusive bounds use the boolean form, as JsonSchema.toDocumentDraft04 already does. On top of that:

  • $schema: not written, since a Schema Object has none. OpenAPI 3.0 identifies the dialect through the document's openapi field rather than per schema.
  • Definitions: a root definitions with #/definitions/... references, as in the draft-07 branch. 3.0 has neither $defs nor definitions, so a consumer moves them to components/schemas either way, as @nestjs/swagger does. zod writes the same for this target.
  • examples: the first one becomes example.
  • null: 3.0 has no "null" type. A { type: "null" } branch of an anyOf becomes nullable: true on the first branch with a single type, and null is added to that branch's enum, if it has one. When annotations are the only siblings, the remaining branch replaces the anyOf. Since 3.0.3, nullable only applies next to type, and it would also let null through keywords like allOf, not or $ref. So when no branch qualifies, as in NullOr of a schema with an identifier, null is kept as a { enum: [null] } branch, which JsonSchema.fromSchemaOpenApi3_0 reads back unchanged. type arrays are split the same way.
  • Tuples: 3.0's items must be a single schema, so prefixItems throws Cannot convert JSON Schema keyword "prefixItems" to OpenAPI 3.0. This follows how the other converters treat keywords the target can't represent, instead of approximating with items: { anyOf } like some other libraries. $anchor, $id, dependentRequired, dependentSchemas and patternProperties throw the same way.

The converter isn't public. A public JsonSchema.toMultiDocumentOpenApi3_0 would reference #/components/schemas/<name>, as JsonSchema.Document describes for OpenAPI 3.0, and could reuse the internal toMultiDocumentComponents that JsonSchema.toMultiDocumentOpenApi3_1 uses for key sanitization on top of the same adapter.

New tests in toStandardJSONSchemaV1.test.ts cover input and output, a nullable string, literal, union and reference, null, a literal, exclusive bounds, definitions, a tuple, and that other targets still throw. JsonSchema.test.ts covers the converter directly, including type arrays, enums, siblings and rejected keywords. 24 of the new tests fail without the change.

Validation

  • pnpm vitest run --project effect packages/effect/test/schema packages/effect/test/JsonSchema.test.ts (2,838 tests passed)
  • pnpm lint
  • pnpm check
  • The @nestjs/swagger@12.1.3 document and bundle sizes above

cc @gcanti

@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ce2bdbd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 43 packages
Name Type
effect Minor
@effect/opentelemetry Minor
@effect/vitest Minor
@effect/ai-amazon-bedrock Minor
@effect/ai-anthropic Minor
@effect/ai-cloudflare Minor
@effect/ai-openai-compat Minor
@effect/ai-openai Minor
@effect/ai-openrouter Minor
@effect/ai-typesafe Minor
@effect/atom-react Minor
@effect/atom-solid Minor
@effect/atom-vue Minor
@effect/chat-discord Minor
@effect/chat-slack Minor
@effect/chat-teams Minor
@effect/chat-telegram Minor
@effect/chat-whatsapp Minor
@effect/platform-browser Minor
@effect/platform-bun Minor
@effect/platform-cloudflare Minor
@effect/platform-deno Minor
@effect/platform-node-shared Minor
@effect/platform-node Minor
@effect/sql-clickhouse Minor
@effect/sql-d1 Minor
@effect/sql-libsql Minor
@effect/sql-mssql Minor
@effect/sql-mysql2 Minor
@effect/sql-pg Minor
@effect/sql-pglite Minor
@effect/sql-sqlite-bun Minor
@effect/sql-sqlite-do Minor
@effect/sql-sqlite-node Minor
@effect/sql-sqlite-react-native Minor
@effect/sql-sqlite-wasm Minor
@effect/docgen Minor
@effect/doctest Minor
@effect/graphql-generator Minor
@effect/openapi-generator Minor
@effect/ui-devtools Minor
@effect/ui-native Minor
@effect/ui-opentui Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 40.54 KB 40.54 KB 0.00 KB (0.00%)
basic.ts 6.57 KB 6.57 KB 0.00 KB (0.00%)
batching.ts 11.80 KB 11.80 KB 0.00 KB (0.00%)
brand.ts 6.91 KB 6.91 KB 0.00 KB (0.00%)
cache.ts 12.06 KB 12.06 KB 0.00 KB (0.00%)
config.ts 23.78 KB 23.78 KB 0.00 KB (0.00%)
differ.ts 22.65 KB 22.65 KB 0.00 KB (0.00%)
http-client.ts 28.82 KB 28.82 KB 0.00 KB (0.00%)
http-router.ts 39.49 KB 39.49 KB 0.00 KB (0.00%)
logger.ts 12.25 KB 12.25 KB 0.00 KB (0.00%)
metric.ts 10.07 KB 10.07 KB 0.00 KB (0.00%)
optic.ts 7.14 KB 7.14 KB 0.00 KB (0.00%)
pubsub.ts 16.81 KB 16.81 KB 0.00 KB (0.00%)
queue.ts 13.61 KB 13.61 KB 0.00 KB (0.00%)
schedule.ts 12.37 KB 12.37 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.40 KB 14.40 KB 0.00 KB (0.00%)
schema-binary.ts 43.36 KB 43.36 KB 0.00 KB (0.00%)
schema-class.ts 22.41 KB 22.41 KB 0.00 KB (0.00%)
schema-codegen.ts 33.78 KB 33.78 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 30.87 KB 30.87 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 28.59 KB 28.59 KB 0.00 KB (0.00%)
schema-string-transformation.ts 16.17 KB 16.17 KB 0.00 KB (0.00%)
schema-string.ts 12.69 KB 12.69 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.77 KB 16.77 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 40.09 KB 40.09 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 21.54 KB 21.54 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 21.71 KB 21.71 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.81 KB 21.81 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.83 KB 26.83 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.88 KB 21.88 KB 0.00 KB (0.00%)
schema.ts 21.52 KB 21.52 KB 0.00 KB (0.00%)
stm.ts 14.87 KB 14.87 KB 0.00 KB (0.00%)
stream.ts 10.62 KB 10.62 KB 0.00 KB (0.00%)
supervisor.ts 12.04 KB 12.04 KB 0.00 KB (0.00%)

UI Bundle Size

Vite 8.4.0/Rolldown, production, default Oxc minification, es2023, ES modules, gzip level 9 per chunk. effect 4.2.0, Node v26.12.1. Effect-only client: 27325 B gzip (Δ 0 B vs base).

App Chunks Raw bytes Gzip bytes Added gzip B Δ raw bytes Δ gzip bytes
counter 1 175435 60490 33165 0 0
features 1 185087 63984 36659 0 0
menu 1 289583 98619 71294 0 0

@tim-smart
tim-smart requested a review from gcanti December 2, 2026 19:42
@effect-bot
effect-bot force-pushed the v4/next-minor branch 26 times, most recently from 6c238bc to 7d60609 Compare December 3, 2026 02:10
@effect-bot
effect-bot force-pushed the v4/next-minor branch 19 times, most recently from 53431e4 to 8a22a76 Compare December 3, 2026 08:30
@MichaelDeBoey
MichaelDeBoey force-pushed the feat/openapi-3-0-standard-json-schema branch 2 times, most recently from 511d3a4 to ce2bdbd Compare December 3, 2026 10:59
@effect-bot
effect-bot force-pushed the v4/next-minor branch 5 times, most recently from 3d3c36a to 6d09256 Compare December 3, 2026 21:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants